package com.java2020.online_mall.dao;

import com.alibaba.druid.util.StringUtils;
import com.java2020.online_mall.model.Admin;
import com.java2020.online_mall.model.AdminPwd;
import com.java2020.online_mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackgeName: com.java2020.online_mall.dao
 * @Author: MNHK
 * Date: 2020/4/28 22:57
 * @Version:1.8
 */

public class AdminDaoImpl implements AdminDao {

    @Override
    public int login(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            Admin ad = queryRunner.query("select * from admin where email = ? and pwd = ?",
                    new BeanHandler<>(Admin.class),
                    admin.getEmail(),
                    admin.getPwd());
            if (ad != null) {
                return 200;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 500;

    }

    @Override
    public List<Admin> allAdmins() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins= queryRunner.query("select * from admin",
                    new BeanListHandler<Admin>(Admin.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    /**
     * 构建动态Sql语句
     * @param params
     * @return
     */
    @Override
    public List<Admin> getSearchAdmins(Admin params) {
        Map<String,Object> dynamicSql = getDynamicSqlParams(params);
        String sql = (String) dynamicSql.get("sql");
        List<String> paramsList = (List<String>) dynamicSql.get("params");
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> adminList = null;
        try {
            adminList = queryRunner.query(sql, new BeanListHandler<Admin>(Admin.class),
                    paramsList.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return adminList;
    }

    /**
     * 拼接动态Sql语句
     * @param params
     * @return
     */
    private Map<String,Object> getDynamicSqlParams(Admin params) {
        String baseSql = "select * from admin where 1 = 1 ";
        //其中where 1 = 1是为了间接拼出动态Sql语句
        List<String> list = new ArrayList<>();
        if(!StringUtils.isEmpty(params.getEmail())){
            baseSql = baseSql + " and email like ?";
            list.add("%" + params.getEmail() + "%");
        }
        if(!StringUtils.isEmpty(params.getNickname())){
            baseSql = baseSql + " and nickname like ?";
            list.add("%" + params.getNickname() + "%");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("sql", baseSql);
        map.put("params", list);
        return map;
    }

    @Override
    public int deleteAdmins(String id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("delete from admin where id = ?", id);
        } catch (SQLException e) {
            e.printStackTrace();
            return 501;
        }
        return 200;
    }

    @Override
    public Admin getAdminsInfo(String id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Admin admin = null;
        try {
            admin = queryRunner.query("select * from admin where id = ?",
                    new BeanHandler<Admin>(Admin.class),
                    id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public int updateAdmins(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("update admin set email = ? , nickname = ? , pwd = ? where id = ?",
                    admin.getEmail(),
                    admin.getNickname(),
                    admin.getPwd(),
                    admin.getId());
        } catch (SQLException e) {
            return 500;
        }
        return 200;
    }

    @Override
    public int addAdmins(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            queryRunner.update("insert into admin values (null,?,?,?)",
                    admin.getEmail(),
                    admin.getNickname(),
                    admin.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
            return 500;
        }
        return 200;
    }

    @Override
    public int changePwd(AdminPwd adminPwd) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Admin admin = null;
        try {
            admin = queryRunner.query("select * from admin where nickname = ?",
                    new BeanHandler<Admin>(Admin.class),
                    adminPwd.getAdminToken());
        } catch (SQLException e) {
            e.printStackTrace();
            return 501;
        }
        if(! admin.getPwd().equals(adminPwd.getOldPwd())){
            return 404;
        }
        try {
            queryRunner.update("update admin set pwd = ? where nickname = ?",
                    adminPwd.getNewPwd(),
                    adminPwd.getAdminToken());
        } catch (SQLException e) {
            e.printStackTrace();
            return 500;
        }
        return 200;
    }
}
